// 入口文件
console.log('OK')
import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
import { Header, Swipe, SwipeItem, Button, Lazyload, Switch } from 'mint-ui';
Vue.component(Header.name, Header);
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Button.name, Button);
Vue.use(Lazyload);
Vue.component(Switch.name, Switch);



import router from './router.js'
import app from './App.vue';

import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'
//导入时间格式插件
import moment from 'moment';
//定义全局时间过滤器
Vue.filter('dateFormat', function (dateStr, pattern = "YYYY-MM-DD HH:mm:ss") {
  return moment(dateStr).format(pattern);
})

//导入网络请求模块
import VueResource from 'vue-resource';
Vue.use(VueResource);
Vue.http.options.root = 'http://127.0.0.1:5001/';

//图片缩略图插件
import VuePreview from 'vue-preview'
// defalut install
Vue.use(VuePreview)
// with parameters install
Vue.use(VuePreview, {
  mainClass: 'pswp--minimal--dark',
  barsSize: { top: 0, bottom: 0 },
  captionEl: false,
  fullscreenEl: false,
  shareEl: false,
  bgOpacity: 0.85,
  tapToClose: true,
  tapToToggleControls: false
})

var car = JSON.parse(localStorage.getItem('car') || '[]');//获取本地存储的car对象
//启用vuex
import Vuex from 'vuex';
Vue.use(Vuex);
var store = new Vuex.Store({
  state: {
    car: car//储存购物车信息
  },
  mutations: {
    addToCar(state, goodsinfo) {//添加商品到购物车
      console.log(goodsinfo);
      var flag = false;
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.count += parseInt(goodsinfo.count);//添加购物车，只是商品的数量改变
          flag = true;
          return true;
        }
      })
      if (!flag) {//store中不存在
        state.car.push(goodsinfo);
      }
      //更新car数据后，把car存储到localStorage中
      localStorage.setItem('car', JSON.stringify(state.car));
    },
    updateGoodsInfo(state, goodsinfo) {//修改购物车中国商品数量
      console.log(goodsinfo);
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.count = parseInt(goodsinfo.count);
          return true;
        }
      })
      //更新数据到localstorage中
      localStorage.setItem('car', JSON.stringify(state.car));
    },
    removeFromCar(state, id) {//删除商品
      state.car.some((item, i) => {
        if (item.id == id) {
          state.car.splice(i, 1);
          return true;
        }
      })
      //s删除完数据，把数据到localstorage中
      localStorage.setItem('car', JSON.stringify(state.car));
    },
    updateGoodsSelected(state,info){//更新购物车中商品的
      state.car.some(item=>{
        if(item.id == info.id){
          item.selected = info.selected;
        }
      })
      //更新完状态，保存到本地localstorage
      localStorage.setItem('car',JSON.stringify(state.car));
    }
  },
  getters: {
    getAllCount(state) {//获取所有购买数量
      var c = 0;
      state.car.forEach(item => {
        c += item.count;
      })
      return c;
    },
    getGoodsCount(state) {//获取商品数量 拼接一个对象，把购物车中的数据都拼接成{id:count,id:count}
      var o = {};
      state.car.forEach(item => {
        o[item.id] = item.count;
      })
      return o;
    },
    getGoodsSelected(state) {//拼接一个对象，把购物车中对象设置为{id:selected,id:selected}
       var o = [];
       state.car.forEach(item=>{
         o[item.id] = item.selected;
       })
       return o;
    },
    getGoodsCountAndAmount(state){//获取商品勾选的数量和总价
        var o = {
          count:0,//勾选的数量
          amount:0//勾选的总价
        }
        state.car.forEach(item=>{
          if(item.selected){
            o.count += item.count;
            o.amount += item.price * item.count;
          }
        })
        return  o;
    }
  }
})

var vm = new Vue({
  el: '#app',
  render: c => c(app),
  router,
  store
})